<?php
    
    namespace App\Observers;

    use App\System\Base\Repositories\BaseNotificationRecordRepository;
    use App\System\Cis\Repositories\CisUserRepository;
    use App\System\Log\Repositories\LogAdminRepository;
    use App\System\Order\Model\OrderInfo;

    class OrderInfoObserver
    {

        /**
         * 监听用户更新的事件。
         *
         * @param  OrderInfo  $orderInfo
         * @return void
         */
        public function updated(OrderInfo $orderInfo)
        {
                $data['type'] = 1;
                $data['log_info'] = '编辑订单详情';
                $logAdmin = LogAdminRepository::getInstance();
                $logAdmin->saveAdminLog($data);
                if($orderInfo->status > 1)
                {
                    $this->addNotification($orderInfo);
                }
        }

        public function created(OrderInfo $orderInfo)
        {
            $this->addNotification($orderInfo);
        }

        private function addNotification($orderInfo)
        {
            $notification = BaseNotificationRecordRepository::getInstance();
            $user = CisUserRepository::getInstance()->getUser($orderInfo->user_id);
            $title = [1=>'订单已创建',2=>'订单已付款',3=>'订单已发货',4=>'订单已收货',5=>'订单已取消'];
            $set = ['title'=>$title[$orderInfo->status],'content'=>'','type'=>2,'from_id'=>$orderInfo->id,'user_id'=>$user->parent_id];
            $notification->addData($set);
        }
    }
     
    